package main

import (
	"fmt"
	"math/rand"
	"sorting-algorithm/sortutil"
	"time"
)

const (
	length = 100000 // 数组长度 & 数值 0 ~ length
)

// 控制是否打印出排序后的数组
var canPrint = false

func main() {
	array := initArray()
	testBubbleSort(array)
	testSelectionSort(array)
	testInsertionSort(array)
	testRandomQuickSort(array)
	testMergeSort(array)
	testHeapSort(array)
}

func initArray() []int {
	rand.Seed(time.Now().Unix())
	l := rand.Intn(length)
	fmt.Println("数组长度为:", l)
	array := make([]int, l, l)
	for i := 0; i < l; i++ {
		array[i] = rand.Intn(length)
	}
	fmt.Println("原始数组:")
	fmt.Println(array)
	return array
}

func testBubbleSort(arr []int) {
	a1 := copyArray(arr)
	start := time.Now()
	sortutil.BubbleSort(a1)
	fmt.Println("冒泡排序: 消耗时间", time.Since(start))
	if canPrint {
		fmt.Println(a1)
	}
}

func testSelectionSort(arr []int) {
	a1 := copyArray(arr)
	start := time.Now()
	sortutil.SelectionSort(a1)
	fmt.Println("直接选择排序: 消耗时间", time.Since(start))
	if canPrint {
		fmt.Println(a1)
	}
}

func testInsertionSort(arr []int) {
	a1 := copyArray(arr)
	start := time.Now()
	sortutil.InsertionSort(a1)
	fmt.Println("直接插入排序: 消耗时间", time.Since(start))
	if canPrint {
		fmt.Println(a1)
	}
}

func testRandomQuickSort(arr []int) {
	a1 := copyArray(arr)
	start := time.Now()
	sortutil.RandomQuickSort(a1)
	fmt.Println("随机快速排序: 消耗时间", time.Since(start))
	if canPrint {
		fmt.Println(a1)
	}
}

func testMergeSort(arr []int) {
	a1 := copyArray(arr)
	start := time.Now()
	sortutil.MergeSort(a1)
	fmt.Println("归并排序: 消耗时间", time.Since(start))
	if canPrint {
		fmt.Println(a1)
	}
}

func testHeapSort(arr []int) {
	a1 := copyArray(arr)
	start := time.Now()
	sortutil.HeapSort(a1)
	fmt.Println("堆排序: 消耗时间", time.Since(start))
	if canPrint {
		fmt.Println(a1)
	}
}

func copyArray(src []int) []int {
	arr := make([]int, len(src), len(src))
	copy(arr, src)
	return arr
}
